package com.example.dmservice.excelInput.service;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.example.dmcommom.Enum.ElectricityCategoryEnum;
import com.example.dmcommom.SpringBeanHelper;
import com.example.dmcommom.StringUtils;
import com.example.dmcommom.TimeUtil;
import com.example.dmdomain.ElectricityPriceRepository;
import com.example.dmdomain.entity.ElectricityPrice;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;

import javax.crypto.Cipher;
import java.math.BigDecimal;
import java.util.*;

@Slf4j
public class ElectricityPriceDataListener extends AnalysisEventListener<Map<Integer, String>> {
	private static final int PROVINCE_COL_NUM = 0;
	private static final int CITY_COL_NUM = 1;
	private static final int ELECTRICITY_CATE_COL_NUM = 2;
	private static final int TYPE_CATE_COL_NUM = 3;
	private static final int VOLTAGE_COL_NUM = 4;
	private static final int CONSUMPTION_COL_NUM = 5;
	private static final int DATE_COL_NUM = 6;
	private static final int PRICE_SHARP_COL_NUM = 7;
	private static final int PRICE_PEEK_COL_NUM = 8;
	private static final int PRICE_SHOULDER_COL_NUM = 9;
	private static final int PRICE_OFF_PEEK_COL_NUM = 10;
	private static final int PRICE_RAVINE_COL_NUM = 11;

	private static final int BATCH_COUNT = 100;
	private List<ElectricityPrice> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

	@Override
	public void invoke(Map<Integer, String> data, AnalysisContext context) {
		if (data.isEmpty()) {
			return;
		}
		if (Strings.isEmpty(JSON.toJSONString(data)) || "{}".equals(JSON.toJSONString(data))) {
			return;
		}
		log.info("解析到一条数据:{}", JSON.toJSONString(data));
		ElectricityPrice price = new ElectricityPrice();
		setValue(data, price);
		cachedDataList.add(price);
		if (cachedDataList.size() >= BATCH_COUNT) {
			saveData();
			cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
		}

	}

	private static void setValue(Map<Integer, String> data, ElectricityPrice price) {
		price.setProvince(data.get(PROVINCE_COL_NUM));
		// 市没有数据，用省代替
		if (StringUtils.isEmpty(data.get(CITY_COL_NUM))) {
			price.setCity(data.get(PROVINCE_COL_NUM));
		} else {
			price.setCity(data.get(CITY_COL_NUM));
		}
		price.setElectricityCategory(ElectricityCategoryEnum.fromString(data.get(ELECTRICITY_CATE_COL_NUM)));
		price.setType(data.get(TYPE_CATE_COL_NUM) != null ? data.get(TYPE_CATE_COL_NUM) : data.get(ELECTRICITY_CATE_COL_NUM));
		price.setVoltage(data.get(VOLTAGE_COL_NUM));
		price.setConsumption(data.get(CONSUMPTION_COL_NUM));
		if (data.get(PRICE_SHARP_COL_NUM) != null && StringUtils.isNumber(data.get(PRICE_SHARP_COL_NUM))) {
			price.setPriceSharp(new BigDecimal(data.get(PRICE_SHARP_COL_NUM)));
		}
		if (data.get(PRICE_PEEK_COL_NUM) != null && StringUtils.isNumber(data.get(PRICE_PEEK_COL_NUM))) {
			price.setPricePeek(new BigDecimal(data.get(PRICE_PEEK_COL_NUM)));
		}
		if (data.get(PRICE_SHOULDER_COL_NUM) != null && StringUtils.isNumber(data.get(PRICE_SHOULDER_COL_NUM))) {
			price.setPriceShoulder(new BigDecimal(data.get(PRICE_SHOULDER_COL_NUM)));
		}
		if (data.get(PRICE_OFF_PEEK_COL_NUM) != null && StringUtils.isNumber(data.get(PRICE_OFF_PEEK_COL_NUM))) {
			price.setPriceOffPeek(new BigDecimal(data.get(PRICE_OFF_PEEK_COL_NUM)));
		}
		if (data.get(PRICE_RAVINE_COL_NUM) != null && StringUtils.isNumber(data.get(PRICE_RAVINE_COL_NUM))) {
			price.setPriceRavine(new BigDecimal(data.get(PRICE_RAVINE_COL_NUM)));
		}
		Calendar c = TimeUtil.strYMDFormat(data.get(DATE_COL_NUM));
		if (c != null) {
			price.setYear(c.get(Calendar.YEAR));
			price.setMonth(c.get(Calendar.MONTH) + 1);
		}
	}

	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
		saveData();
		cachedDataList.clear();
		log.info("所有数据解析完成！");

	}

	private void saveData() {
		log.info("{}条数据，开始存储数据库！", cachedDataList.size());
		ElectricityPriceRepository repository = SpringBeanHelper.getBean(ElectricityPriceRepository.class);
		if (repository != null) {
			repository.saveAll(cachedDataList);
		}
	}

}
